﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace ZHDJ.Data.Models.Center
{
    /// <summary>
    /// 预约阵地
    /// </summary>
    public class PartyPossyAppointment : EntityBase
    {
        #region 属性


        /// <summary>
        /// 预约的阵地ID
        /// </summary>

        public string PossyID { get; set; }

        /// <summary>
        /// 申请人组织id
        /// </summary>
        public string BranchID { get; set; }

        /// <summary>
        /// 预约人
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 电话号码
        /// </summary>
        public string MobileNo { get; set; }

        /// <summary>
        /// 预约时间
        /// </summary>
        public DateTime? AppointmentTime { get; set; }

        /// <summary>
        /// 说明
        /// </summary>
        public string Describe { get; set; }

        /// <summary>
        /// 状态 0：未审核  1：同意   2：不同意 3：取消
        /// </summary>
        public int Status { get; set; }

        /// <summary>
        /// 申请组织
        /// </summary>
        public string ApplyBranch { get; set; }

        /// <summary>
        /// 参观时间段
        /// 09:00-10:00
        /// 10:30-11:30
        /// 14:30-15:30
        /// 16:00-17:00
        /// </summary>
        public string AppointmentTimeSpen { get; set; }

        /// <summary>
        /// 参观人数
        /// </summary>
        public int PeopleCount { get; set; }

        /// <summary>
        /// 带领人员
        /// </summary>
        public string Leader { get; set; }

        /// <summary>
        /// 带领人员工作单位
        /// </summary>
        public string LeaderUnit { get; set; }

        /// <summary>
        /// 带领人员联系单位
        /// </summary>
        public string LeaderMobile { get; set; }

        /// <summary>
        /// 带领人员职务
        /// </summary>
        public string LeaderPosition { get; set; }

        /// <summary>
        /// 来访事由 1、了解天河智慧党建。2、过组织生活。3、党员学习、支部活动。4、其他。
        /// </summary>
        public string VisitReason { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public string CreateBy { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 修改人
        /// </summary>
        public string ModifyBy { get; set; }
        #endregion

        #region 查询管理
        /// <summary>
        /// 获取GetDataGrid数据源
        /// </summary>
        /// <returns></returns>
        public static DataSet GetGridData(int pageIndex, int pageSize, string userName, string status, string applyUser = "", string possyadmin = "")
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {

                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize}
            };

            StringBuilder sbSql = new StringBuilder();
            sbSql.Append(@" select  SQL_CALC_FOUND_ROWS   Data.*,po.Name as PossyName,
                                case Data.Status when '3' then '已取消' when '1' then '同意'
                        when '2' then '不同意' when '0' then '未审核' else '草稿' end as StatusName
                                 from tblPartyPossyAppointment Data
                                left join tblPartyPossyInfo po on po.ID = Data.PossyID  
                                where 1 = 1 ");

            if (!string.IsNullOrEmpty(userName))
            {
                sbSql.Append(" and Data.userName like ?userName ");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "userName", Value = string.Format("%{0}%", userName) });
            }


            if (!string.IsNullOrEmpty(applyUser))
            {
                sbSql.Append(" and Data.CreateBy = ?applyUser ");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "applyUser", Value = applyUser });
            }

            if (!string.IsNullOrEmpty(status))
            {
                sbSql.Append(" and Data.status = ?status ");
                parameters.Add(new MySqlParameter() { DbType = DbType.Int32, ParameterName = "status", Value = status });
            }

            if (!string.IsNullOrEmpty(possyadmin))
            {
                sbSql.Append(" and po.PossyAdmin like ?PossyAdmin ");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "PossyAdmin", Value = string.Format("%{0}%", possyadmin) });
            }

            if (string.IsNullOrEmpty(applyUser))
            {
                sbSql.Append(" order by Data.status,Data.AppointmentTime desc");
            }
            else
            {
                sbSql.Append(" order by Data.AppointmentTime desc");
            }
            sbSql.Append("  LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sbSql.ToString(), parameters.ToArray());

        }


        ///// <summary>
        ///// 获取数据统计
        ///// </summary>
        ///// <returns></returns>
        //public static DataSet GetDataCount(string Name, string branchID = "", string StartTime = "", string EndTime = "", string LoginID = "")
        //{
        //    using (SqlCommand cmd = new SqlCommand())
        //    {
        //        StringBuilder sbSql = new StringBuilder();
        //        sbSql.Append(@" select ppi.Name,ob.BranchName,(CONVERT(varchar(100),Data.AppointmentTime, 23)+' '+Data.AppointmentTimeSpen) as Time,Data.UserName,Data.Describe, Data.* from tblPartyPossyAppointment Data
        //                        left join tblPartyPossyInfo ppi on data.PossyID=ppi.PossyID
        //                        left join tblPartyOrgBranch ob on data.BranchID=ob.BranchID
        //                      where 1 = 1 ");
        //        if (!string.IsNullOrEmpty(Name))
        //        {
        //            sbSql.Append(" and ppi.Name like @userName ");
        //            cmd.Parameters.Add("@userName", SqlDbType.VarChar).Value = string.Format("%{0}%", Name);
        //        }

        //        if (!string.IsNullOrEmpty(branchID) && string.IsNullOrEmpty(LoginID))
        //        {
        //            sbSql.Append(" and Data.BranchID in (select ID from f_BranchID(@BranchID)) ");
        //            cmd.Parameters.Add("@BranchID", SqlDbType.VarChar).Value = string.Format("{0}", branchID);
        //        }
        //        else if (!string.IsNullOrEmpty(branchID) && !string.IsNullOrEmpty(LoginID))
        //        {
        //            sbSql.Append(" and Data.BranchID = @BranchID ");
        //            cmd.Parameters.Add("@BranchID", SqlDbType.VarChar).Value = string.Format("{0}", branchID);
        //        }
        //        if (!string.IsNullOrEmpty(StartTime))
        //        {
        //            sbSql.Append(" and Data.AppointmentTime>=@StartTime");
        //            cmd.Parameters.Add("@StartTime", SqlDbType.DateTime).Value = Convert.ToDateTime(StartTime);
        //        }
        //        if (!string.IsNullOrEmpty(EndTime))
        //        {
        //            sbSql.Append(" and Data.AppointmentTime<=@EndTime");
        //            cmd.Parameters.Add("@EndTime", SqlDbType.DateTime).Value = Convert.ToDateTime(EndTime);
        //        }
        //        if (!string.IsNullOrEmpty(LoginID))
        //        {
        //            sbSql.Append(" and ppi.PossyAdmin like @PossyAdmin ");
        //            cmd.Parameters.Add("@PossyAdmin", SqlDbType.VarChar).Value = string.Format("%{0}%", LoginID);
        //        }
        //        sbSql.Append(" order by Data.AppointmentTime desc");

        //        cmd.CommandText = sbSql.ToString();
        //        return DBManager.DefaultDB.ExecuteDataSet(cmd);
        //    }
        //}
        #endregion
    }
}
